package cn.lixu.pet.service.impl;

import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.pet.domain.Pet;
import cn.lixu.pet.domain.PetDetail;
import cn.lixu.pet.mapper.PetDetailMapper;
import cn.lixu.pet.mapper.PetMapper;
import cn.lixu.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void save(Pet pet) {
        //先添加到PetMapper，返回自增主键
        petMapper.save(pet);

        //再添加到PetDetailMapper
        PetDetail petDetail = pet.getPetDetail(); //获取PetDetail对象
        petDetail.setPet_id(pet.getId());//对PetDetail的petid进行重新赋值
        petDetailMapper.save(petDetail);//添加到petDetailMapper
    }

    @Override
    public void remove(Long id) {
        petMapper.remove(id);
        petDetailMapper.removePetDetailByPetId(id);
    }

    /**
     * 宠物上架
     *
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        Date date = new Date();
        petMapper.onSale(ids, date);
    }

    /**
     * 宠物下架
     *
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        Date date = new Date();
        petMapper.offSale(ids, date);
    }
}

